﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommonExceptioin;
using Architecture.Events;
using System.Transactions;
using Data;
using Entity;
using DynamicProxy;
using Castle.DynamicProxy;
using System.IO;
using System.Xml.Serialization;
using Commons.Web;
using ConfigCache;
using Architecture.Helpers;
using DynamicProxy.IInterceptor;

namespace Architecture.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            DynamicProxy.DynamicProxyContain.Instance<HomeController, HomeController>(new LogInterceptorProxy("日志写入成功...")).Print();

            Recharge();
            #region 声明实体并赋值
            var product_Comment = new Product_Comment
            {
                CommentInfo = "test",
                CommentTitle = "Test",
                ProductID = 1,
                UserID = 1
            };
            var review = new Review
            {
                CreateDate = DateTime.Now.Date,
                Info = "test",
                ObjID = 1,
                ObjType = 1,
                Status = 100,
                UserID = 1,
            };
            var product = new Product
            {
                Info = "test",
                Name = "test",
                Review = review,
                Product_Comment = product_Comment
            };
            #endregion

            #region 事件订阅与存储
            OrderEventArgs afterEventHandler = new OrderEventArgs();
            afterEventHandler.After += (handler) =>
            {
                new VLog.TxtVLog().Error(handler.UserID.ToString() + handler.Price.ToString());
                new ProductRepository().Insert(product);
            };

            Session["system"] = afterEventHandler;
            #endregion

            try
            {
                int a = 0;
                int b = 10 / a;
            }
            catch (Exception e)
            {
                if (!new DefaultExceptionPolicy().HandleException(null, e))
                    throw;
            }
            return View();
        }

        #region WEB层的方法拦截

        public virtual void Print()
        {
         //   this.ControllerContext.HttpContext.Response.Write("输出内容呀");
        }
        /// <summary>
        /// 可能是某个动作，如充值
        /// </summary>
        /// <returns></returns>
        public void Recharge()
        {
            Response.Write("Print前的方法");
            Print();//被拦截的方法
            Response.Write("Print后的方法");
        }
        #endregion

        public ActionResult Detail()
        {
            #region 事件触发
            OrderEventArgs afterEventHandler = Session["system"] as OrderEventArgs;
            afterEventHandler.UserID = 1;
            afterEventHandler.Price = 100m;
            afterEventHandler.OnAfter(afterEventHandler);
            #endregion
            return View();
        }

        public ActionResult Create()
        {

            return View();
        }
        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            var product_Comment = new Product_Comment
            {
                CommentInfo = form["CommentInfo"],
                CommentTitle = form["CommentTitle"],
                ProductID = 1,
                UserID = 1
            };
            new Product_CommentRepository().Insert(product_Comment);
            return View();
        }

        [HttpPost]
        public ActionResult Create2(string commentInfo, string commentTitle)
        {
            var product_Comment = new Product_Comment
            {
                CommentInfo = commentInfo,
                CommentTitle = commentTitle,
                ProductID = 1,
                UserID = 1
            };
            new Product_CommentRepository().Insert(product_Comment);
            return View();
        }

        public ContentResult GetInfo()
        {
            return Content("收到信息！");
        }
    }


}
